package com.gs.api.util.video.parse.douyin;

import cn.hutool.http.HttpUtil;
import com.alibaba.fastjson.JSONObject;
import com.gs.api.util.video.parse.ImageParse;
import com.gs.api.util.video.parse.ParseConstant;
import com.gs.api.util.video.parse.ParseUtil;
import com.gs.api.util.video.parse.vo.VideoVo;
import org.jsoup.Jsoup;

import java.util.List;

/**
 * @author guoyunfei on 2022/4/14 16:05
 * @className DouyinApp
 * @description TODO 抖音APP
 * @Version 1.0
 */
public class DouyinApp {

    /**
     * 抖音解析入口
     * @param url
     * @return 抖音图集，视频地址
     */
    public static VideoVo douYinParseUrl(String url) {
        VideoVo videoVo = new VideoVo();
        try {
            /**
             *  加上/video/这个7位数
             *  获取 视频ID = 获取/video/ 和 / 中间的数据
             */
            String itemId = ParseUtil.resp(url).split("/video/")[1].split("/")[0];

            /**
             * （固定）https://www.iesdouyin.com/web/api/v2/aweme/iteminfo/?item_ids=
             *  videoUrl = 视频链接 + 视频ID
             */
            String jsonStr = Jsoup.connect(ParseConstant.douYinParseUrl + itemId).ignoreContentType(true).execute().body();

            JSONObject obj = JSONObject.parseObject(jsonStr);

            JSONObject itemList = obj.getJSONArray("item_list").getJSONObject(0);

            // 获取当前的地址的解析类型
            String aweme_type = itemList.getString("aweme_type");

            if("2".equals(aweme_type)){
                List<String> images = ImageParse.douYinParseData(obj);
                videoVo.setType(ParseConstant.PARSE_IMAGES);
                videoVo.setImages(images);

            }else if ("4".equals(aweme_type)){
                String videoSrc = douYinParseData(obj);
                videoVo.setVideoSrc(videoSrc);
                videoVo.setType(ParseConstant.PARSE_VIDEO);
            }

            //获取封面
            String coverSrc = itemList.getJSONObject("video").
                    getJSONObject("origin_cover").getJSONArray("url_list").get(0).toString();
            videoVo.setCoverSrc(coverSrc);

            //文案
            String desc = obj.getJSONArray("item_list").getJSONObject(0).getString("desc");
            videoVo.setCopywriting(desc);

            //音频地址
            String play_url = itemList.getJSONObject("music").getJSONObject("play_url").getString("uri");
            videoVo.setAudioSrc(play_url);
            //获取文案
        }catch (Exception e){
            e.printStackTrace();
            videoVo.setStatus(500);
        }
        return videoVo;
    }


    /**
     * 抖音视频
     */
    public static String douYinParseData(JSONObject obj) {
        String videoUrl = "";
        try {

            // 获取当前的视频的真实url
            String videoAddress = obj.getJSONArray("item_list").getJSONObject(0).getJSONObject("video").getJSONObject("play_addr").getJSONArray("url_list").get(0).toString();
            /**
             * https://aweme.snssdk.com/aweme/v1/playwm/?video_id=
             * 把playwm替换成play
             */
            videoAddress = videoAddress.replaceAll("playwm", "play");
            // 该视频的原地址
            videoUrl = HttpUtil.createGet(videoAddress).addHeaders(ParseUtil.headers()).execute().header("Location");
        } catch (Exception e) {
            e.printStackTrace();
        }
        return videoUrl;
    }

}
